//
//  XuanCityTableViewController.m
//  解析
//
//  Created by lanou3g on 16/1/15.
//  Copyright © 2016年 WHB. All rights reserved.
//

#import "XuanCityTableViewController.h"
#import "City.h"
#import "Nuomishuo.h"
#import "CityTableViewCell.h"
//城市
NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/cities";
NSString *httpArg = @"";

@interface XuanCityTableViewController ()

@property (nonatomic,strong)NSMutableDictionary *dataDic;
@property (nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation XuanCityTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"城市选择";
    _dataArray = [NSMutableArray array];
    self.dataDic = [NSMutableDictionary dictionary];

    [self addself];
    [self.tableView registerNib:[UINib nibWithNibName:@"CityTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
 
}

-(void)addself{
    
    Nuomishuo *manger= [[Nuomishuo alloc]init];
    [manger request:httpUrl withHttpArg:httpArg];
       __weak  typeof(manger)temp = manger;
    manger.upData =^(){
        for (City *model in temp.array) {
//            NSLog(@"====%@",model);
//            NSLog(@"dic===%@",_dataDic);
        NSString *grouKey = [model.city_pinyin substringWithRange:NSMakeRange(0, 1)];
        if (self.dataDic[grouKey] == nil) {
//            NSLog(@"%@",_dataDic[grouKey]);
            NSMutableArray *groupArray = [NSMutableArray array];
            [groupArray addObject:model];
            [self.dataDic setObject:groupArray forKey:grouKey];
//            NSLog(@"%@",_dataDic[grouKey]);
        }else{
            NSMutableArray *groupArray = self.dataDic[grouKey];
//            NSLog(@"%@",self.dataDic);
            [groupArray addObject:model];
        }
//          [_dataArray addObjectsFromArray:[_dataDic.allKeys]];
    }
     
            self.dataArray = [NSMutableArray arrayWithArray:_dataDic.allKeys];
       
        [self.dataArray sortUsingSelector:@selector(compare:)];
        NSLog(@"%@",_dataArray);
        [self.tableView reloadData];
//        NSLog(@"%@",_dataDic);
    };
    self.tableView.tableHeaderView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/5)];
    
}
   
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"%ld",_dataArray.count);
    return _dataArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = _dataDic[_dataArray [section]];
    return array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    NSArray *array = [_dataDic objectForKey:_dataArray[indexPath.section]];
    City *model = array[indexPath.row];
    cell.city = model;
    NSLog(@"%@",model.city_name);
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

//快速索引
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return _dataArray;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return _dataArray[section];
}
//传到控制器id
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array = [_dataDic objectForKey:_dataArray[indexPath.section]];
    City *model = array[indexPath.row];
    //city_id 传值接口
    NSLog(@"%@", [NSString stringWithFormat:@"%@",model.city_id]);
   

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
